<?php

class HtmlTableRenderer {
	
	function Render( &$oConfiguration, &$oTable ) {
		
		$sText = HtmlTag::H( 2, $oTable->GetName(), $oTable->GetName() );
		
		$sText .= HtmlTableRenderer::RenderColumns( $oConfiguration, $oTable->GetColumns() );
		$sText .= HtmlTableRenderer::RenderPrimaryKey  ( $oConfiguration, $oTable->GetPrimaryKey() );
		$sText .= HtmlTableRenderer::RenderReferencedBy( $oConfiguration, $oTable->GetForeignKeysReferencingThisTable() );
		$sText .= HtmlTableRenderer::RenderReferences  ( $oConfiguration, $oTable->GetForeignKeysFromThisTable() );

		return $sText;
	}
	
	function RenderColumns( &$oConfiguration, &$aColumns ) {

		if ( !$oConfiguration->GetProperty( 'ShowColumns' ) ) {
			return '';
		}

		$sText = '';

		$sRow   = HtmlTag::Td( 'Name', 'ColumnNameTitle'     );
		$sRow  .= HtmlTag::Td( 'Datatype', 'ColumnDataTypeTitle' );
		$sText .= HtmlTag::Tr( $sRow, 'TitleRow' );
		
		foreach ( $aColumns as $oThisColumn ) {
			$sRow   = HtmlTag::Td( $oThisColumn->GetName()    , 'ColumnName'     );
			$sRow  .= HtmlTag::Td( $oThisColumn->GetDataType(), 'ColumnDataType' );
			$sText .= HtmlTag::Tr( $sRow );
		}
		$sText = HtmlTag::Table( $sText, 'TableColumns' );

		return $sText;
	}

	function RenderPrimaryKey( &$oConfiguration, &$oPrimaryKey ) {

		if ( !$oConfiguration->GetProperty( 'ShowPrimaryKeys' ) ) {
			return '';
		}


		if ( is_object( $oPrimaryKey ) ) {

			$sColumnNames = implode( ', ', $oPrimaryKey->GetColumnNames() );
			$sText = 'Primary Key: ' . $oPrimaryKey->GetName() . ' consists of ' . $sColumnNames;
			return HtmlTag::P( $sText, 'PrimaryKey' );
		}
	}
	
	function RenderReferencedBy( &$oConfiguration, &$aForeignKeys ) {
		
		if ( !$oConfiguration->GetProperty( 'ShowForeignKeys' ) ) {
			return '';
		}
		
		$sText = HtmlTag::H( 3, 'Referenced By' );
		if ( $aForeignKeys ) {
			foreach( $aForeignKeys as $oThisForeignKey ) {
				$sText .= HtmlTableRenderer::RenderThisReferencedBy( $oConfiguration, $oThisForeignKey );
			}
		} else {
			$sText .= HtmlTag::P( 'None' );
		}
		return $sText;
	}

	function RenderThisReferencedBy( &$oConfiguration, &$oForeignKey ) {
		
		if ( ! $oConfiguration->GetProperty('ShowForeignKeysToFilteredTables' ) && $oForeignKey->IsFromAFilteredTable() ) {
			return '';	
		}

		$sKey = $oForeignKey->GetName() . ' exists on ';
		$sKey .= HtmlTableRenderer::MakeTableNameLink( $oConfiguration, $oForeignKey->GetFromTableName(), $oForeignKey->IsFromAFilteredTable() );
		return HtmlTag::P( $sKey );
		
	}

	function RenderReferences( &$oConfiguration, &$aForeignKeys ) {
		
		if ( !$oConfiguration->GetProperty( 'ShowForeignKeys' ) ) {
			return '';
		}
		
		$sKeysText = '';

		if ( $aForeignKeys ) {
			foreach( $aForeignKeys as $oThisForeignKey ) {
				$sKeysText .= HtmlTableRenderer::RenderThisReference( $oConfiguration, $oThisForeignKey );
			}
		}
		
		if ( ! $sKeysText ) {
			$sKeysText .= HtmlTag::P( 'None' );
		}
		
		return HtmlTag::H( 3, 'References' ) . $sKeysText;
	}
	
	function RenderThisReference( &$oConfiguration, &$oForeignKey ) {
		
		if ( ! $oConfiguration->GetProperty('ShowForeignKeysToFilteredTables' ) && $oForeignKey->IsToAFilteredTable() ) {
			return '';	
		}
		
		$sColumnNameList = implode( ', ', $oForeignKey->GetColumnNames() );
		$sKey = $oForeignKey->GetName() . ' (' . $sColumnNameList . ') references ';
		$sKey .= HtmlTableRenderer::MakeTableNameLink( $oConfiguration, $oForeignKey->GetToTableName(), $oForeignKey->IsToAFilteredTable() );

		return HtmlTag::P( $sKey );
	}
	
	function MakeTableNameLink( &$oConfiguration, $sTableName, $bFilteredTable = false ) {
		
		if ( ! $oConfiguration->GetProperty( 'IncludeTableLinks') ) {
			return $sTableName;
		}

		$sUrl = false;
		if   ( !$bFilteredTable || $oConfiguration->GetProperty( 'IncludeTableLinksToFilteredTables') ) {
			if ( $oConfiguration->GetProperty( 'BaseTableLinkUrl') ) {
				$sUrl = $oConfiguration->GetProperty( 'BaseTableLinkUrl') . $sTableName;
			}
			$sTableName = HtmlTag::Anchor( $sTableName, $sUrl );
			
		}
		return $sTableName;
	}
}

?>
